package cn.sfturing.web;

import cn.sfturing.entity.*;
import cn.sfturing.service.*;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller(value = "AdminController")
@RequestMapping(value = "/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private ControlService controlService;
    @Autowired
    private OfficeService officeService;
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private OrderRecordsService orderRecordsService;


    //登录验证
    @RequestMapping(value = "/login")
    public String login(Model model, HttpServletRequest request){

        String adminName = request.getParameter("AdName");
        String adminPassword = request.getParameter("passWord");
        Boolean aBoolean = adminService.verifyAdmin(new Admin(null,adminName,adminPassword,null));
        if(aBoolean){
            //用户存在的话
            Admin admin = adminService.queryByName(adminName);
            if ("停用".equals(admin.getState())) {
                model.addAttribute("loginState",-1);
                return "forward:../index.jsp";
            }
            Control control = controlService.selectByuserId(admin.getAdid());
            if(control == null){
                admin.setRank(-1000);
                request.getSession().setAttribute("adminSession",admin);
                return "bk/AdminHome";
            }
            admin.setHospitalName(control.getHospitalName());
            admin.setRank(control.getRank());
            request.getSession().setAttribute("adminSession",admin);
            //保存医院对应的科室
            List<Office> officeList=new ArrayList<>();
            if(control.getHospitalName()!=null&&control.getHospitalName()!=""){
                officeList = officeService.findOfficeByHosName(control.getHospitalName());
            }
            request.getSession().setAttribute("officeList",officeList);
            return "bk/AdminHome";
        }else {
            model.addAttribute("loginState",0);
            return "forward:../index.jsp";
        }
    }

    @RequestMapping(value = "/logout.html", method = RequestMethod.GET)
    public String logout(HttpServletRequest request){
        request.getSession().setAttribute("adminSession", null);
        return "forward:../index.jsp";
    }

    @RequestMapping(value = "/logout2.html", method = RequestMethod.GET)
    public String logout2(HttpServletRequest request){
        request.getSession().setAttribute("doctorSession", null);
        return "forward:../index.jsp";
    }

    //后台专用
    @RequestMapping(value = "admin/toAdd.html")
    public String toAdd(){
        return "bk/adminadd";
    }

    //管理员列表
    @RequestMapping(value = "/list.html")
    public String list(Admin admin, Model model){
        List<Admin> adminList = adminService.queryAll(admin);
        model.addAttribute("admins",adminList);
        model.addAttribute("qAdmin",admin);
        return "bk/adminlist";
    }

    @RequestMapping(value = "/toList")
    public String toList(){
        return "bk/adminlist";
    }

    @RequestMapping(value = "/login0")
    public String login0(){
        return "bk/AdminHome";
    }
    
    @RequestMapping(value = "/toWelcome")
    public String toWelcome(){
        return "bk/welcome";
    }

    @RequestMapping(value = "/addSave.json",method = RequestMethod.GET)
    @ResponseBody
    public Object addAdmin(@RequestParam String AdName, @RequestParam String password, @RequestParam String state){
        HashMap<String,String> map = new HashMap<String, String>();
        return JSONArray.toJSONString(map);
    }
    //删除账号
    @RequestMapping(value = "/delAdmin.json", method = RequestMethod.GET)
    @ResponseBody
    public Object delAdmin(@RequestParam String id){
        HashMap<String,String> map = new HashMap<String, String>();
        if(id == null || id == ""){
            map.put("delResult","notexist");
            return JSONArray.toJSONString(map);
        }
        if(adminService.del(Integer.parseInt(id))){
            map.put("delResult","true");
        }else{
            map.put("delResult","false");
        }
        return JSONArray.toJSONString(map);
    }
    //修改权限
    @RequestMapping(value = "/updateState.json", method = RequestMethod.GET)
    @ResponseBody
    public Object updataState(@RequestParam String id, @RequestParam String state){
        HashMap<String,String> map = new HashMap<String, String>();
        if(id == null || id == ""){
            map.put("delResult","notexist");
            return JSONArray.toJSONString(map);
        }
        if(adminService.updatePassword(new Admin(Integer.parseInt(id),null,null,state))){
            map.put("updateResult","true");
            map.put("state",state);
        }else{
            map.put("updateResult","false");
        }
        return JSONArray.toJSONString(map);
    }

    @RequestMapping(value = "/toModifyPwd")
    public String toModifyPwd(){
        return "bk/adminUpdataPassword";
    }
    //修改密码
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    public String UpdataPassword(@RequestParam("password") String password, @RequestParam("newpassword") String newpassword, HttpServletRequest request, Model model){
        Admin admin = (Admin)request.getSession().getAttribute("adminSession");
        System.out.println("========================="+password+"================"+newpassword+"==============="+admin.getPassword());
        if(null == admin) {
            return "forward:../index.jsp";
        }
        if(!admin.getPassword().equals(password)){
            model.addAttribute("updatePasswordResult","false");
            return "bk/adminUpdataPassword";
        }
        if(adminService.updatePassword(new Admin(admin.getAdid(),admin.getAdName(),newpassword,null))){
            model.addAttribute("updatePasswordResult","true");
        }else{
            model.addAttribute("updatePasswordResult","false");
        }
        return "bk/adminUpdataPassword";
    }


    /**
     * 添加管理员
     */
    @RequestMapping(value = "/toAdd.html", method = RequestMethod.GET)
    public String toAdd1() {
        return "bk/adminadd";
    }

    /**
     * 保存管理员
     */
    @RequestMapping(value = "/addSave.json", method = RequestMethod.POST)
    @ResponseBody
    public Object upDataSave(@RequestBody Admin admin){
        Control control = new Control();
        control.setRank(admin.getRank());
        control.setHospitalName(admin.getHospitalName());

        //数据处理
        HashMap<String, String> resultMap = new HashMap<String, String>();
        if(adminService.addAdmin(admin)){
            List<Admin> newAdmin = adminService.queryAll(admin);
            control.setUserId(newAdmin.get(0).getAdid());
            if(controlService.insertSelective(control)){
                resultMap.put("addResult", "true");
            }
        }else{
            resultMap.put("addResult", "false");
        }
        return JSONArray.toJSONString(resultMap);
    }

    //医生登录
    @RequestMapping(value = "/logindoctor", method = RequestMethod.POST)
    public String login(HttpServletRequest request, @RequestParam("dId") Integer dId, @RequestParam("dPassword") String dPassword, Model model){
        Doctor doctor1 = new Doctor();
        doctor1.setPasswrod(dPassword);
        doctor1.setId(dId);
        Doctor doctor = doctorService.loginDoctor(doctor1);
        if(null != doctor){
            model.addAttribute("doctor",doctor);
            request.getSession().setAttribute("doctorSession", doctor);
            return "bk/doctorhome";
        }else {
            model.addAttribute("loginState",0);
            return "forward:../index.jsp";
        }
    }

    //医生查订单列表
    @RequestMapping(value = "/doctor/list.html")
    public String doctorByList(Model model,@RequestBody Doctor doctor){
        OrderRecords orderRecords = new OrderRecords();
        orderRecords.setHospitalName(doctor.getHospitalName());
        orderRecords.setOfficesName(doctor.getOfficesName());
        orderRecords.setDoctorName(doctor.getDoctorName());
        orderRecordsService.queryBk(orderRecords);
        return "bk/doctorbyList";
    }
}



